package com.xykj.enterprise.wechat.event.service;

import com.xykj.enterprise.wechat.bean.ext.externalcontact.ExternalContactFollowUser;
import com.xykj.enterprise.wechat.bean.ext.externalcontact.ExternalContactVo;
import com.xykj.enterprise.wechat.bean.ext.externalcontact.External_contact;
import com.xykj.enterprise.wechat.bean.ext.externalcontact.External_contact_list;
import com.xykj.enterprise.wechat.bean.ext.externalcontact.Follow_info;
import com.xykj.enterprise.wechat.bean.ext.externalcontact.GetExternalContactBatchVo;
import com.xykj.enterprise.wechat.core.dao.CorpExtDao;
import com.xykj.enterprise.wechat.core.dao.customer.CustomerDao;
import com.xykj.enterprise.wechat.core.dao.customer.CustomerFollowUserDao;
import com.xykj.enterprise.wechat.core.model.dodb.CorpExt;
import com.xykj.enterprise.wechat.core.model.dodb.Customer;
import com.xykj.enterprise.wechat.core.model.dodb.CustomerFollowUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author zhouxu
 * @create 2021-04-08 09:55
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerDao customerDao;
    @Autowired
    private CorpExtDao corpExtDao;
    @Autowired
    private CustomerFollowUserDao customerFollowUserDao;

    @Override
    public void syncCustomerList(GetExternalContactBatchVo batchVo, String corpid, String userid) {
        if (batchVo != null && !batchVo.getExternal_contact_list().isEmpty()) {
            for (External_contact_list vo : batchVo.getExternal_contact_list()) {
                External_contact contact = vo.getExternal_contact();
                // customer 同步
                Customer c = customerDao.get(corpid, contact.getExternal_userid());
                if (c == null) {
                    c = customerDao.save(
                            corpid,
                            contact.getExternal_userid(),
                            contact.getName(),
                            contact.getPosition(),
                            contact.getAvatar(),
                            String.valueOf(contact.getType()),
                            contact.getAvatar(),
                            contact.getUnionid(),
                            contact.getCorp_name(),
                            contact.getCorp_full_name(),
                            null,
                            null,
                            null

                    );
                } else {
                    c = customerDao.update(
                            c.getId(),
                            corpid,
                            contact.getExternal_userid(),
                            contact.getName(),
                            contact.getPosition(),
                            contact.getAvatar(),
                            String.valueOf(contact.getType()),
                            contact.getAvatar(),
                            contact.getUnionid(),
                            contact.getCorp_name(),
                            contact.getCorp_full_name(),
                            null,
                            null,
                            null

                    );
                }

                Follow_info info = vo.getFollow_info();
                // follow_user 同步
                CustomerFollowUser followUser = customerFollowUserDao.get(c.getExternalUserid(), c.getCorpid());
                if (followUser == null) {
                    customerFollowUserDao.save(
                            c.getCorpid(),
                            c.getExternalUserid(),
                            c.getId(),
                            userid,
                            info.getRemark(),
                            info.getDescription(),
                            info.getCreatetime(),
                            info.getRemark_corp_name(),
                            info.getRemark_mobiles().toString(),
                            info.getOper_userid(),
                            info.getAdd_way(),
                            null,
                            info.getTag_id().toString()
                    );
                } else {
                    customerFollowUserDao.update(
                            followUser.getId(),
                            c.getCorpid(),
                            c.getExternalUserid(),
                            c.getId(),
                            userid,
                            info.getRemark(),
                            info.getDescription(),
                            info.getCreatetime(),
                            info.getRemark_corp_name(),
                            info.getRemark_mobiles().toString(),
                            info.getOper_userid(),
                            info.getAdd_way(),
                            null,
                            info.getTag_id().toString()
                    );
                }
            }
        }

        // 同步完成标记
        corpExtDao.syncCustomerData(corpid, CorpExt.SUCC);
    }

    @Override
    public void save(String corpid, ExternalContactVo contact) {
        // 1.新增/修改 客户记录
        Customer c = customerDao.get(corpid, contact.getExternal_userid());
        if (c == null) {
            c = customerDao.save(
                    corpid,
                    contact.getExternal_userid(),
                    contact.getName(),
                    contact.getPosition(),
                    contact.getAvatar(),
                    String.valueOf(contact.getType()),
                    contact.getAvatar(),
                    contact.getUnionid(),
                    contact.getCorp_name(),
                    contact.getCorp_full_name(),
                    null,
                    null,
                    null

            );
        } else {
            c = customerDao.update(
                    c.getId(),
                    corpid,
                    contact.getExternal_userid(),
                    contact.getName(),
                    contact.getPosition(),
                    contact.getAvatar(),
                    String.valueOf(contact.getType()),
                    contact.getAvatar(),
                    contact.getUnionid(),
                    contact.getCorp_name(),
                    contact.getCorp_full_name(),
                    null,
                    null,
                    null

            );
        }

        // 2.先新增，再新增员工关系记录
        customerFollowUserDao.batchDelete(contact.getExternal_userid(), corpid);
        List<ExternalContactFollowUser> followUserList = contact.getFollow_user();
        if (followUserList != null) {
            c = customerDao.save(
                    corpid,
                    contact.getExternal_userid(),
                    contact.getName(),
                    contact.getPosition(),
                    contact.getAvatar(),
                    String.valueOf(contact.getType()),
                    contact.getAvatar(),
                    contact.getUnionid(),
                    contact.getCorp_name(),
                    contact.getCorp_full_name(),
                    null,
                    null,
                    null

            );
        }
    }

    @Override
    public void delete(String corpid, String externalUserid) {
        // 1.删除客户
        customerDao.delete(corpid, externalUserid);
        // 2.删除followUser
        customerFollowUserDao.batchDelete(externalUserid, corpid);
    }

    @Override
    public void deleteFollowUser(String corpid, String externalUserid, String userid) {
        CustomerFollowUser followUser = customerFollowUserDao.get(externalUserid,corpid,userid);
        customerFollowUserDao.delete(followUser);

    }


}
